import { Link } from "../../app_base/module/structure/Link";


type ConditionStruct = {
    condition: string
}

/**
 * 条件数据
 */
export class ConditionLink extends Link<ConditionStruct>{
    _conditonHash = null

    constructor() {
        super("condition", { condition: "" });
    }

    protected _restSelfData(): void {
        this._data.condition = "";
    }

    get conditonHash() {
        if (this._conditonHash == null) {
            this._conditonHash = {}
            let arr = this.condition.split(",")
            for (let index = 0; index < arr.length; index++) {
                this._conditonHash[arr[index]] = true
            }
        }
        return this._conditonHash
    }

    protected get condition(): string {
        return this._data.condition;
    }

    protected set condition(value: string) {
        this._data.condition = value;
    }

    /**
     * 添加完成的条件
     * @param condition 
     */
    public addCompletCondition(tag: string): void {
        let saveId = tag.split("_")[1]
        if (this.conditonHash[saveId] == true) return

        let oldValue = this.condition

        //更新数据
        if (this.condition == "")
            this.condition = saveId
        else
            this.condition = this.condition + "," + saveId
        this.updated("condition", this.condition, oldValue)

        //记录hash
        this.conditonHash[saveId] = true
    }

    /**
     * 是否完成条件
     * @param condition 
     * @returns 
     */
    public isCompletCondition(tag: string): boolean {
        let hash = this.conditonHash
        return hash[tag.split("_")[1]] == true
    }
}